package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.ClueFalseDto;
import com.qk.dto.CluePool;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {

    @Autowired
    private ClueService clueService;

    //添加线索
    @PostMapping
    public Result addClue(@RequestBody Clue clue) {
        clue.setStatus(1);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clueService.save(clue);
        return Result.success();
    }

    //查询线索
    @GetMapping
    public Result getAllClues(ClueDto clueDto) {
        PageResult<Clue> pageResult = clueService.selectPage(clueDto);
        return Result.success(pageResult);
    }


    @PutMapping("/assign/{clueId}/{userId}")
    public Result assignClue(@PathVariable Integer clueId, @PathVariable Integer userId) {
        log.info("clueId:{},userId:{}", clueId, userId);
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setStatus(2);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getClueById(@PathVariable Integer id) {
       Clue clue = clueService.getClueById(id);
       return Result.success(clue);
    }

    @PutMapping
    public Result updateClue(@RequestBody Clue clue) {
        clueService.updateByClueId(clue);
        return Result.success();

    }

    @PutMapping("/false/{id}")
    public Result falseClue(@PathVariable Integer id,@RequestBody ClueFalseDto clueFalseDto) {
        clueService.updateFalseClue(id, clueFalseDto);
        return Result.success();
    }

    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id){
        log.info("id:{}", id);
        clueService.toBusiness(id);
        return Result.success();
    }

    //线索池
    @GetMapping("/pool")
    public Result pool(CluePool cluePool) {
        PageResult<Clue> pageResult = clueService.listPage(cluePool);
        return Result.success(pageResult);
    }


}
